<template>
<!-- 注册页 -->
  <div>
    <component-head/>
     <div class="text">注册</div>
    <van-form
    style="
    margin-top: 2rem;"
    @submit="onSubmit">
      <van-field
        v-model="user.username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' }
        ]"
      />
      <van-field
        v-model="user.pwd"
        type="password"
        name="pwd"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' },
        {
            pattern: /^[A-Za-z0-9]{6,12}$/,
            message: '请输入6~12位数字或字母'
          }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { userRegister } from '_api'
import { Toast } from 'vant'
import ComponentHead from '../../components/ComponentHead.vue'
export default {
  data () {
    return {
      user: {
        username: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (params) {
      console.log(params)
      userRegister(params).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          // 弹出提示 告知用户注册成功 并重定向 到 登录页
          Toast.success({
            message: '注册成功',
            duration: 1000,
            onClose: () => {
              // 去登录页
              this.$router.replace('/login')
            }
          })
        } else {
          Toast.fail('注册失败')
        }
      })
    }
  },
  components: { ComponentHead }
}
</script>

<style lang="scss" scoped>
.text{
  margin-top: 2rem;
  text-align: center;
  color: rgb(128, 100, 100);
}
</style>
